package com.liantong.service.cms;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.liantong.common.dao.HallPictureDao;
import com.liantong.common.mapper.BusinessHallMapper;
import com.liantong.common.mapper.HallPictureMapper;
import com.liantong.common.po.App;
import com.liantong.common.po.BusinessHall;
import com.liantong.common.po.BusinessHallExample;
import com.liantong.common.po.HallPicture;
import com.liantong.common.po.HallPictureExample;
import com.liantong.common.vo.InsertHallInfo;
import com.liantong.service.util.AppUuidGenerator;
import com.liantong.service.util.FileUtil;
import com.liantong.util.MD5Util;

@Service("businessHallService")
public class BusinessHallService  {
	
	@Resource(name="businessHallMapper")
	private BusinessHallMapper bussinessHallMapper;

	@Resource(name="hallPictureMapper")
	private HallPictureMapper hallPictureMapper;
	
	@Resource(name="hallPictureDao")
	private HallPictureDao hallPictureDao;
	
	@Transactional
	public void insertHall(InsertHallInfo insertHallInfo) throws Exception {

		BusinessHall hall = insertHallInfo.getHall();
		hall.setId(AppUuidGenerator.generate());
		copyResource(hall, insertHallInfo);
		bussinessHallMapper.insert(hall);
	}
	
	public List<BusinessHall> getAll(){
		BusinessHallExample example = new BusinessHallExample();
		example.createCriteria().andIdIsNotNull();
		List<BusinessHall> result = bussinessHallMapper.selectByExample(example);
		
		for (BusinessHall r : result) {
			r.setPhoto(FileUtil.getRealPath(r.getPhoto()));
			HallPictureExample pe = new HallPictureExample();
			pe.createCriteria().andBusinessHallIdEqualTo(r.getId());
			List<HallPicture> list = hallPictureMapper.selectByExample(pe);
			List<String> paths = new ArrayList<String>();
			for(HallPicture p : list){
				paths.add(FileUtil.getRealPath(p.getResPath()));
			}
			r.setPictures(paths);
		}
		return result;
	}
		
	
	private void copyResource(BusinessHall hall, InsertHallInfo insertHallInfo) throws Exception {
		List<String> picturePath = new ArrayList<String>();

		String id = String.valueOf(hall.getId());

		String contextPath = insertHallInfo.getContextPath();

		String photoFilePath = copyFile(id, insertHallInfo.getPhotoPath(), contextPath, insertHallInfo.getPhotoName(),
				insertHallInfo.getPhoto());
		hall.setPhoto(photoFilePath);

		picturePath = copyFiles(id, insertHallInfo.getPictures(), insertHallInfo.getPictureName(), contextPath,
				insertHallInfo.getPicturePath());


		addPicture(hall.getId(), picturePath);
	}
	
	private String copyFile(String appId, String toParentFilePath, String toContentPath, String fromFileName, File fromFile) throws Exception {

		String toFileName = "";
		try {
			toFileName = toParentFilePath + FileUtil.getDestinationFilePath(appId, MD5Util.getMD5(fromFile), fromFileName);
			FileUtils.copyFile(fromFile, new File(toContentPath + toFileName));

		} catch (IOException e) {
			throw new Exception(e);
		}
		return toFileName;
	}
	
	private List<String> copyFiles(String id, File[] files, String[] fileName, String contextPath, String filePath) throws Exception {

		List<String> result = new ArrayList<String>();

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			String name = fileName[i];

			String previewFilePath = copyFile(id, filePath, contextPath, name, file);
			result.add(previewFilePath);
		}

		return result;
	}
	
	private void addPicture(String id, List<String> preview) {
		List<HallPicture> list = new ArrayList<HallPicture>();

		for (String respath : preview) {
			HallPicture pv = new HallPicture();

			pv.setId(AppUuidGenerator.generate());
			pv.setBusinessHallId(id);
			pv.setResPath(respath);

			list.add(pv);			
		}
		hallPictureDao.batchInsert(list);		
	}

}
